<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <!--              班级下拉框-->
              <a-select
                show-search
                placeholder="选择班级"
                option-filter-prop="children"
                style="width: 100%"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
                v-model="model.classId"
              >
                <a-select-option v-for="(item,key) in clsList" :key=key :value="item.classId">
                  {{item.className}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="面试人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuId">
              <!--              学生下拉框-->
              <a-select
                show-search
                placeholder="面试学生"
                option-filter-prop="children"
                style="width: 100%"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                v-model="model.stuId"
              >
                <a-select-option v-for="(item,key) in stuList" :key=key :value="item.stuId">
                  {{item.stuName}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="面试老师" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherId">
              <!--              老师下拉框-->
              <a-select
                show-search
                placeholder="面试老师"
                option-filter-prop="children"
                style="width: 100%"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                v-model="model.teacherId"
              >
                <a-select-option v-for="(item,key) in teacherList" :key=key :value="item.teacherId">
                  {{item.teacherName}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="面试时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mockInterviewTime">
              <j-date placeholder="请选择面试时间"  v-model="model.mockInterviewTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="12">
            <a-form-model-item label="老师意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teacherAdvise">
              <j-editor v-model="model.teacherAdvise" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="学生总结" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stuConclusion">
              <j-editor v-model="model.stuConclusion" />
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MockInterviewForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          /* classId: [
              { required: true, message: '请输入班级!'},
           ],
           stuId: [
              { required: true, message: '请输入面试人!'},
           ],
           teacherId: [
              { required: true, message: '请输入面试老师!'},
           ],*/
        },
        url: {
          add: "/mockinterview/mockInterview/add",
          edit: "/mockinterview/mockInterview/edit",
          queryById: "/mockinterview/mockInterview/queryById",
          getStuListUrl:"/stumiddlecls/stuMiddleCls/CheckClsName",
          getTeacherListUrl:"/teacher/teacher/getTeacherList",
          getClsListUrl:"/cls/cls/getClsList"
        },
        stuList:[],
        teacherList:[],
        clsList:[],
        classId:''
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getTeacherList()
      this.getClsList()
    },
    methods: {
      /*下拉框方法*/
      handleChange(classId) {
        /*查询学生*/
          getAction(this.url.getStuListUrl+"/"+classId).then((resp)=>{
            if (resp.success){
              this.stuList=resp.result
            }
          })
      },
      handleBlur() {
        console.log('blur');
      },
      handleFocus() {
        console.log('focus');
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },

      /*查询老师*/
      getTeacherList(){
        getAction(this.url.getTeacherListUrl).then((resp)=>{
          if (resp.success){
            this.teacherList=resp.result
          }
        })
      },
      /*查询班级*/
      getClsList(){
        getAction(this.url.getClsListUrl).then((resp)=>{
          if (resp.success){
            this.clsList=resp.result
          }
        })
      },

      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>